package apps.softtech.fastmarket;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 
 * @author MUI
 * 
 */
public class ActivityEditUserAccount extends Activity {

	protected static final int SELECT_PICTURE = 1;
	EditText edtPass, edtPhone, edtEmail, edtAddress;
	TextView tvEnterPass;
	Button btnEdit, btnEmail, btnPhone, btnAddress, btnPass;
	ImageView ivAvatar;
	Context mContext;
	private Bitmap mBitmap;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_edit_user);
		mContext = this;
		init();
		getEvent();
	}

	private void getEvent() {

		ivAvatar.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setType("image/*");
				intent.setAction(Intent.ACTION_GET_CONTENT);
				startActivityForResult(
						Intent.createChooser(intent, "Select Picture"),
						SELECT_PICTURE);
			}
		});

		btnEmail.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				edtEmail.setEnabled(true);
				edtEmail.requestFocus();
			}
		});

		btnAddress.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				edtAddress.setEnabled(true);
				edtAddress.requestFocus();
			}
		});


		btnPass.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				displayDialog();
			}
		});

		btnEdit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (checkValidate()) {
					Toast.makeText(mContext, "validate ok", Toast.LENGTH_SHORT)
							.show();
				} else {
					Toast.makeText(mContext, " not validate",
							Toast.LENGTH_SHORT).show();
				}
			}
		});
	}

	/**
	 * display dialog edit password
	 */

	private void displayDialog() {
		final Dialog dialog = new Dialog(mContext);
		dialog.setContentView(R.layout.dialog_new_password);
		dialog.setTitle(getResources().getString(R.string.change_pass));

		final EditText edtNewPass = (EditText) dialog
				.findViewById(R.id.edtNewPass);
		final EditText edtNewPass2 = (EditText) dialog
				.findViewById(R.id.edtNewPass2);
		final TextView tvAlear = (TextView) dialog.findViewById(R.id.tvAlert);
		Button btnOk = (Button) dialog.findViewById(R.id.btnOk);
		Button btnCancel = (Button) dialog.findViewById(R.id.btnCancel);

		btnCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});

		btnOk.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String newpass = edtNewPass.getText().toString();
				if (newpass.equals(edtNewPass2.getText().toString())) {
					tvAlear.setVisibility(View.VISIBLE);
					tvAlear.setText("Mật khẩu không khớp");
				} else if (!checkPassword(newpass)) {
					tvAlear.setVisibility(View.VISIBLE);
					tvAlear.setText("password invalid");
				}

			}
		});

		dialog.show();
	}

	private Boolean checkEmail(String st) {
		String email_pattern = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
				+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
		Pattern pattern;
		Matcher matcher;
		pattern = Pattern.compile(email_pattern);
		matcher = pattern.matcher(st);
		return matcher.matches();

	}

	private Boolean checkPassword(String password) {

		if (password.equals(null) || password.equals("")
				|| password.length() >= 100 || password.length() <= 6) {
			return false;
		}
		return true;
	}

	private Boolean checkValidate() {
		if (edtEmail.getText().toString().equals("")
				|| edtPhone.getText().toString().equals("")
				|| edtAddress.getText().toString().equals("")) {
			return false;
		}
		if (!checkEmail(edtEmail.getText().toString())) {
			Toast.makeText(mContext,
					getResources().getString(R.string.email_invalid),
					Toast.LENGTH_LONG).show();
			return false;
		}
		if (!checkPassword(edtPass.getText().toString())) {
			Toast.makeText(mContext,
					getResources().getString(R.string.password_invalid),
					Toast.LENGTH_LONG).show();
			return false;
		}
		return true;

	}

	/**
	 * get ImagePath of image was choose from device
	 * 
	 * @param uri
	 * @return
	 */
	public String getPath(Uri uri) {
		String[] projection = { MediaStore.Images.Media.DATA };
		Cursor cursor = managedQuery(uri, projection, null, null, null);
		int column_index = cursor
				.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
		cursor.moveToFirst();
		return cursor.getString(column_index);
	}

	/**
	 * get Bitmap of image in imageView
	 */
	private void getBitmapImageView() {
		Drawable d = ivAvatar.getDrawable();
		mBitmap = ((BitmapDrawable) d).getBitmap();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			if (requestCode == SELECT_PICTURE) {
				Uri selectedImageUri = data.getData();
				String imagepath = getPath(selectedImageUri);
				System.out.println("Image Path : " + imagepath);
				Bitmap bitmap = BitmapFactory.decodeFile(imagepath);
				ivAvatar.setImageBitmap(bitmap);
				getBitmapImageView();

			}
		}
	}
	
	/**
	 * send info User Account to server
	 */

	private void sendInfoToServer(String gcmId, String userId, String userName, 
			String email, String address, String phone) {
		

	}

	private void init() {
		edtPass = (EditText) findViewById(R.id.edtPassword);
		edtAddress = (EditText) findViewById(R.id.edtAddress);
		edtPhone = (EditText) findViewById(R.id.edtPhoneNumber);
		edtEmail = (EditText) findViewById(R.id.edtEmail);

		ivAvatar = (ImageView) findViewById(R.id.ivAvatar);
		tvEnterPass = (TextView) findViewById(R.id.tvEnterPassword);

		btnEdit = (Button) findViewById(R.id.btnEdit);
		btnEmail = (Button) findViewById(R.id.btnEmail);
		btnPhone = (Button) findViewById(R.id.btnPhone);
		btnAddress = (Button) findViewById(R.id.btnAddress);
		btnPass = (Button) findViewById(R.id.btnPassword);
	}

}
